from db.aiomysql_connector import AioMysqlClient
from services.cache_service import CacheUtils


class PLCService:
    table = 'PLC'
    r1_field_list = ['data100', 'data101', 'data102', 'data103', 'data104', 'data105', 'data106', 'data107', 'data108',
                     'data109', 'data110', 'data111', 'data112', 'data113', 'data114', 'data115', 'data116', 'data117',
                     'data118'
                     ]
    r2_field_list = ['data200', 'data201', 'data202', 'data203', 'data204', 'data205', 'data206', 'data207', 'data208',
                     'data209', 'data210', 'data211', 'data212', 'data213', 'data214', 'data215', 'data216', 'data217',
                     'data218', 'data219'

                     ]
    r3_field_list = ['data300', 'data301', 'data302', 'data303', 'data304', 'data305', 'data306', 'data307', 'data308',
                     'data309'

                     ]
    r4_field_list = ['data400', 'data401', 'data402', 'data403', 'data404', 'data405', 'data406', 'data407', 'data408',
                     'data409']
    r5_field_list = ['data500', 'data501', 'data502', 'data503', 'data504', 'data505', 'data506', 'data507', 'data508',
                     'data509', 'data510', 'data511', 'data512', 'data513', 'data514', 'data515', 'data516']
    agv_field_list = ['data700', 'data701', 'data702', 'data703', 'data704', 'data705', 'data706', 'data707', 'data708',
                      'data709', 'data710']
    finished_field_list = ['data801', 'data802', 'data803', 'data804', 'data805', 'data806']
    semi_finished_field_list = ['data900', 'data901', 'data902', 'data903', 'data904', 'data905', 'data906']

    def __init__(self):
        self.sql_client: AioMysqlClient = CacheUtils.get('aiomysql_pool')

    async def get_cur_robot_data(self):
        sql = f"SELECT *  FROM {self.table} ORDER BY id DESC LIMIT 1"
        robot_status_fields = ['data1000', 'data1001', 'data1002', 'data1003', 'data1004', 'data1005', 'data1006',
                               'data1007']
        robot_field_sn_map = {'data1000': 0, 'data1001': 1, 'data1002': 2, 'data1003': 3, 'data1004': 4, 'data1005': 5,
                              'data1006': 6, 'data1007': 7}
        data = await self.sql_client.fetch_one(sql)
        if data:
            last_robot_status_map = CacheUtils.get('robot_status_map')
            cur_robot_status_map = {i: 1 if data[i] and data[i] != str(0) else 0 for i in robot_status_fields}
            CacheUtils.set('robot_status_map', cur_robot_status_map)
            if not last_robot_status_map:
                # 无缓存,第一次查询
                send_datas = []
                for robot, status in cur_robot_status_map.items():
                    r_data = {'sn': robot_field_sn_map[robot], 'status': status}
                    send_datas.append(r_data)
                return send_datas
            #
            send_datas = []
            for robot, status in cur_robot_status_map.items():
                if status != last_robot_status_map[robot]:
                    # 状态改变
                    r_data = {'sn': robot_field_sn_map[robot], 'status': status}
                    send_datas.append(r_data)
        else:
            return []
        return send_datas

    async def r1_robot_datas(self):
        station = 'R1'
        sql = f"SELECT {', '.join(self.r1_field_list)} FROM {self.table} ORDER BY id DESC LIMIT 1"
        data = await self.sql_client.fetch_one(sql)
        if data:
            return {
                'sn': 0,
                f'{station}机器人1轴角度': data['data100'],
                f'{station}机器人2轴角度': data['data101'],
                f'{station}机器人3轴角度': data['data102'],
                f'{station}机器人4轴角度': data['data103'],
                f'{station}机器人5轴角度': data['data104'],
                f'{station}机器人6轴角度': data['data105'],
            }
        return {}

    async def r2_robot_datas(self):
        station = 'R2'
        sql = f"SELECT {', '.join(self.r2_field_list)} FROM {self.table} ORDER BY id DESC LIMIT 1"
        data = await self.sql_client.fetch_one(sql)
        if data:
            return {
                'sn': 1,
                f'{station}机器人1轴角度': data['data200'],
                f'{station}机器人2轴角度': data['data201'],
                f'{station}机器人3轴角度': data['data202'],
                f'{station}机器人4轴角度': data['data203'],
                f'{station}机器人5轴角度': data['data204'],
                f'{station}机器人6轴角度': data['data205'],
            }
        return {}

    async def r3_robot_datas(self):
        station = 'R3'
        sql = f"SELECT {', '.join(self.r3_field_list)} FROM {self.table} ORDER BY id DESC LIMIT 1"
        data = await self.sql_client.fetch_one(sql)
        if data:
            return {
                'sn': 2,
                f'{station}机器人1轴角度': data['data300'],
                f'{station}机器人2轴角度': data['data301'],
                f'{station}机器人3轴角度': data['data302'],
                f'{station}机器人4轴角度': data['data303'],
                f'{station}机器人5轴角度': data['data304'],
                f'{station}机器人6轴角度': data['data305'],
            }
        return {}

    async def r4_robot_datas(self):
        station = 'R4'

        sql = f"SELECT {', '.join(self.r4_field_list)} FROM {self.table} ORDER BY id DESC LIMIT 1"
        data = await self.sql_client.fetch_one(sql)
        if data:
            return {
                'sn': 3,
                f'{station}机器人1轴角度': data['data400'],
                f'{station}机器人2轴角度': data['data401'],
                f'{station}机器人3轴角度': data['data402'],
                f'{station}机器人4轴角度': data['data403'],
                f'{station}机器人5轴角度': data['data404'],
                f'{station}机器人6轴角度': data['data405'],
            }
        return {}

    async def r5_robot_datas(self):
        station = 'R5'
        sql = f"SELECT {', '.join(self.r5_field_list)} FROM {self.table} ORDER BY id DESC LIMIT 1"
        data = await self.sql_client.fetch_one(sql)
        if data:
            return {
                'sn': 4,
                f'{station}机器人1轴角度': data['data500'],
                f'{station}机器人2轴角度': data['data501'],
                f'{station}机器人3轴角度': data['data502'],
                f'{station}机器人4轴角度': data['data503'],
                f'{station}机器人5轴角度': data['data504'],
                f'{station}机器人6轴角度': data['data505'],
            }
        return {}

    async def agv_robot_datas(self):
        station = '复合式AGV'
        sql = f"SELECT {', '.join(self.agv_field_list)} FROM {self.table} ORDER BY id DESC LIMIT 1"
        data = await self.sql_client.fetch_one(sql)
        if data:
            return {
                'sn': 5,
                f'{station}机器人1轴角度': data['data700'],
                f'{station}机器人2轴角度': data['data701'],
                f'{station}机器人3轴角度': data['data702'],
                f'{station}机器人4轴角度': data['data703'],
                f'{station}机器人5轴角度': data['data704'],
                f'{station}机器人6轴角度': data['data705'],
                f'{station}机器人7轴角度': data['data706'],
            }
        return {}

    async def finished_datas(self):
        station = '成品库'
        sql = f"SELECT {', '.join(self.finished_field_list)} FROM {self.table} ORDER BY id DESC LIMIT 1"
        data = await self.sql_client.fetch_one(sql)
        if data:
            return {
                'sn': 6,
                f'{station}一层托盘伸出到位': data['data801'],
                f'{station}一层托盘缩回到位': data['data802'],
                f'{station}二层托盘伸出到位': data['data803'],
                f'{station}二层托盘缩回到位': data['data804'],
                f'{station}三层托盘伸出到位': data['data805'],
                f'{station}三层托盘缩回到位': data['data806'],
            }
        return {}

    async def semi_finished_datas(self):
        station = '半成品库'
        sql = f"SELECT {', '.join(self.semi_finished_field_list)} FROM {self.table} ORDER BY id DESC LIMIT 1"
        data = await self.sql_client.fetch_one(sql)
        if data:
            return {
                'sn': 7,
                f'{station}旋转伺服电机位置': data['data901'],
                f'{station}升降伺服位置': data['data902'],
                f'{station}横移气缸伸出到位': data['data903'],
                f'{station}横移气缸缩回到位': data['data904'],
                f'{station}叉臂气缸伸出到位': data['data905'],
                f'{station}叉臂气缸缩回到位': data['data906'],
            }
        return {}

    methods = [r1_robot_datas, r2_robot_datas, r3_robot_datas, r4_robot_datas, r5_robot_datas, agv_robot_datas,
               finished_datas, semi_finished_datas]
